package org.yan.stock.server.core;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public abstract class DemoMessaging implements Messaging {

	public abstract boolean sendNoRetry(String registrationId, String collapse, Map<String, String[]> params, boolean delayWhileIdle)
			throws IOException;

	public boolean sendNoRetry(String registrationId, String collapseKey, String name1, String value1, String name2, String value2, String name3,
			String value3) throws IOException {

		Map<String, String[]> params = new HashMap<String, String[]>();
		if (value1 != null)
			params.put("data." + name1, new String[] { value1 });
		if (value2 != null)
			params.put("data." + name2, new String[] { value2 });
		if (value3 != null)
			params.put("data." + name3, new String[] { value3 });

		try {
			return sendNoRetry(registrationId, collapseKey, params, true);
		} catch (IOException ex) {
			return false;
		}
	}

	public boolean sendNoRetry(String registrationId, String collapseKey, String... nameValues) throws IOException {

		Map<String, String[]> params = new HashMap<String, String[]>();
		int len = nameValues.length;
		if (len % 2 == 1) {
			len--; // ignore last
		}
		for (int i = 0; i < len; i += 2) {
			String name = nameValues[i];
			String value = nameValues[i + 1];
			if (name != null && value != null) {
				params.put("data." + name, new String[] { value });
			}
		}

		try {
			return sendNoRetry(registrationId, collapseKey, params, true);
		} catch (IOException ex) {
			return false;
		}
	}
}
